<?php

namespace App\V3\V3_2\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LogInStorageModel extends Model
{
    use HasFactory;

    protected $table = 'log_instorage';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    public static function inLogExport(array $data, array $user, bool $startTime, bool $endTime)
    {
        $query = LogInstorageModel::select(
            'a.id',
            'a.type',
            'f.storage_name',
            'f.storage_address',
            'g.storage_name as last_storage_name',
            'g.storage_address as last_storage_address',
            //            'a.materiel_class_name',
            //            'a.materiel_class_no',
            'e.materiel_name',
            'e.type_name',
            'e.materiel_no',
            'e.materiel_img',
            'a.operate_num',
            'a.last_storage_num',
            'a.next_storage_num',
            'a.materiel_mean_cost',
            'a.remark',
            'a.is_cancel',
            'a.allocate_no',
            'a.in_storage_time',
            'a.purchase_no',
            'c.class_name',
            'c.class_no',
            'b.class_name as materiel_class_name',
            'b.class_no as materiel_class_no',
            'd.username',
            'd.nickname',
            'h.location_name',
            'i.shelf_name',
            'a.serial_number',
            'a.created_at',
            'j.batch_no',
            'l.shelf_name as last_shelf_name',
            'm.location_name as last_location_name',
            'n.batch_no as last_batch_no'
        )
            ->from('log_instorage as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->leftJoin('admin as d', 'd.id', '=', 'a.user_id')
            ->leftJoin('materiel_type as e', 'e.id', '=', 'a.materiel_type_id')
            ->leftJoin('storage as f', 'f.id', '=', 'a.storage_id')
            ->leftJoin('storage as g', 'g.id', '=', 'a.last_storage_id')
            ->leftJoin('storage_shelf_location as h','h.id','=','a.location_id')
            ->leftJoin('storage_shelf as i','i.id','=','a.shelf_id')
            ->leftJoin('storage_batch as j', 'j.id', '=', 'a.batch_id')
            ->leftJoin('storage_manage as k', 'k.id', '=', 'a.last_storage_manage_id')
            ->leftJoin('storage_shelf as l', 'l.id', '=', 'k.shelf_id')
            ->leftJoin('storage_shelf_location as m', 'm.id', '=', 'k.location_id')
            ->leftJoin('storage_batch as n', 'n.id', '=', 'a.batch_id')
            ->where('a.delete_flag', 0)
            ->where($user)
            ->where('a.is_cancel', $data['status']);

        if($data['materiel_class_id'] != 0) {
            $query->where('a.materiel_class_id', $data['materiel_class_id']);
        }

        $keyword = empty($data['keyword'])?'':$data['keyword'];

        if(!empty($startTime) && !empty($endTime)) {
            $query->whereBetween('in_storage_time', [$startTime, $endTime]);
        }
        else if(!empty($startTime) && empty($endTime)) {
            $query->where('in_storage_time', '>=', $startTime);
        }
        else if(empty($startTime) && !empty($endTime)) {
            $query->where('in_storage_time', '<=', $endTime);
        }

        return $query->where(function($query) use ($keyword, $data) {
            $query->orWhere('b.class_name', 'like', '%'.$keyword.'%');
            $query->orWhere('b.class_no', 'like', '%'.$keyword.'%');
            $query->orWhere('c.class_name', 'like', '%'.$keyword.'%');
            $query->orWhere('c.class_no', 'like', '%'.$keyword.'%');
            $query->orWhere('e.materiel_name', 'like', '%'.$keyword.'%');
            $query->orWhere('e.type_name', 'like', '%'.$keyword.'%');
            $query->orWhere('e.materiel_no', 'like', '%'.$keyword.'%');
            $query->orWhere('f.storage_name', 'like', '%'.$keyword.'%');
            $query->orWhere('g.storage_name', 'like', '%'.$keyword.'%');

            if($data['type'] == 0) {
                $query->orWhere('purchase_no', 'like', '%'.$keyword.'%');
            }
        })
            ->orderBy('a.id', 'desc')
            ->get()->toArray();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'in_storage_time',
    ];
    public function hasManyContent()
    {
        return self::hasMany(LogPackageContentModel::class, 'log_package_id', 'id');
    }

    //今日套餐总数
    static public function getTodayPackageCount($condition, $startTime, $endTime)
    {
        return self::where($condition)
        ->whereBetween('created_at', [$startTime, $endTime])
        ->count();
    }

    //今日入库总数
    static public function getTodayInCount($condition, $startTime, $endTime)
    {
        return self::where($condition)
            ->where('type', '!=', 1)
            ->whereBetween('created_at', [$startTime, $endTime])
            ->count();
    }
}
